<template>
    <el-select
        v-model="selectedVal"
        :clearable="clearable"
        class="filter-item"
        :collapse-tags="multiple"
        :multiple="multiple"
        :multiple-limit="multipleLimit"
        filterable
        @change="($event) => {this.$emit('change', $event)}"
        :placeholder="'选择怪物' + (multiple ? ', 可多选' : '')"
        :style="(multiple && multipleLimit > 1) ? 'width: 220px;' : 'width: 150px;'">
        <el-option label="未选择" :value="0" v-if="form"></el-option>
        <el-option
                v-for="(item, index) in monsters"
                :key="index"
                :label="item.val.ID +' ) '+ item.val.name"
                :value="item.val.ID">
        </el-option>
    </el-select>
</template>

<script>
    export default {
        props: {
            value: {
                type: [String, Number, Array],
            },
            // 多选
            multiple: {
                type: Boolean,
                default: false,
            },
            multipleLimit: {
                type: Number,
                default: 0,
            },
            clearable: {
                type: Boolean,
                default: true,
            },
            form: {
                type: Boolean,
                default: false,
            },
            // 待过滤ID
            filterIds: {
                type: Array,
                default () {
                    return [];
                }
            },
            // 限制ID
            limitIds: {
                type: Array,
                default () {
                    return [];
                }
            },
        },
        data () {
            return {
                selectedVal: '',
            };
        },
        computed: {
            monsters () {
                return this.$store.state.monster.all.filter(v => {
                    if (this.selectedVal === v.id) return true;
                    if (this.filterIds.length) {
                        return !this.filterIds.includes(v.id);
                    }
                    if (this.limitIds.length) {
                        return this.limitIds.includes(v.id);
                    }
                    return true;
                })
            },
        },
        watch: {
            value() {
                this.syncForm();
            },
            selectedVal() {
                this.$emit('input', this.selectedVal);
            }
        },
        methods: {
            getMonsters() {
                if (!this.monsters.length) this.$store.dispatch('monster/All', 'monster');
            },
            syncForm() {
                this.selectedVal = this.value;
            },
        },
        created() {
            this.getMonsters();
            this.syncForm();
        }
    }
</script>

<style scoped>

</style>
